package com.shen.controller;

import com.shen.service.ContentService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class ContentController {

    @Autowired
    ContentService contentService;


    /**
     * 获取京东商品的数据，并放到es中
     * @param keywords
     * @return
     * @throws IOException
     */
    @GetMapping("/parse/{keywords}")
    public boolean parse(@PathVariable("keywords")String keywords) throws IOException {
        return contentService.parseContent(keywords);
    }

    /**
     * 从es中获取信息
     */
    @GetMapping("/search/{pageNo}/{pageSize}/{keyword}")
    public List<Map<String,Object>> searchPage(@PathVariable("keyword") String keyword,
                                               @PathVariable("pageNo")int pageNo,
                                               @PathVariable("pageSize")int pageSize) throws IOException {
        return contentService.searchPage(keyword,pageNo,pageSize);
    }

}
